<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <script language="javascript" type="text/javascript" src="vue/vue2.x.js"></script>
    <script language="javascript" type="text/javascript" src="elementUI/elementUI2.x.js"></script>
    <!--    <link rel="stylesheet" href="elementUI/elementUI2.x.css" type="text/css">-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="css/login.css" type="text/css">
</head>
<body>
<el-container>
    <div class="container">
        <div id="login">  <!-- method="post" action="/goindex"goindex-->
            <el-form method="post" action="/goindex" :model="ruleForm" status-icon :rules="rules" ref="ruleForm"
                     label-width="100px">
                <el-row>
                    <el-col :span="24"><h1>用户登录</h1></el-col>
                </el-row>
                <!--                后台消息弹框-->
                <div ref="info" v-show="false" th:text="${msg}"></div>
                <div ref="state" v-show="false" th:text="${state}"></div>

                <!-- 分割线-->
                <el-divider></el-divider>
                <el-form-item label="邮箱" prop="user_mail">
                    <el-input v-model="ruleForm.user_mail" name="user_mail"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="user_pwd">
                    <el-input type="password" show-password v-model="ruleForm.user_pwd" name="user_pwd"
                              autocomplete="off"></el-input>
                </el-form-item>
                <!--验证码-->
                <el-form-item label="人机验证">
                    <div class="drag">
                        <div class="bg"></div>
                        <div class="text" onselectstart="return false;">请拖动滑块进行人机验证</div>
                        <div class="btn">&gt;&gt;</div>
                    </div>
                </el-form-item>

                <el-form-item class="loginBtn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <!--用于跳转登录-->
                    <input type="submit" ref="logBtn" v-show="false">
                    <p>还没有账号？去<a href="/goregister">注册</a></p>
                    <p><a href="/goFindPwd">忘记密码？</a>账号未激活？<a href="/goActivation">去激活</a></p>
                </el-form-item>
            </el-form>
        </div>
    </div>
</el-container>

<!--<script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
<script language="javascript" type="text/javascript">
    var validatePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入密码'));
        } else {
            callback();
        }
    };
    //判断验证码是否通过
    var authCode = false;

    var vm = new Vue({
        el: '#login',
        data: {
            ruleForm: {
                user_mail: '',
                user_pwd: '',
            },
            rules: {
                user_pwd: [
                    {required: true, validator: validatePass, trigger: 'blur'}
                ],
                user_mail: [
                    {required: true, message: '请输入邮箱地址', trigger: 'blur'},
                    {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
                ]
            }
        },
        mounted: function () {
            if (this.$refs.info.innerHTML != "") {
                this.$message({
                    message: this.$refs.info.innerHTML,
                    type: 'error'
                });
            }
            if (this.$refs.state.innerHTML != "") {
                this.$message({
                    message: this.$refs.state.innerHTML,
                    type: 'success'
                });
            }
        },
        methods: {
            submitForm: function (formName) {
                this.$refs[formName].validate((valid) => {
                    if (!valid) {
                        console.log('error submit!!');
                        return false;
                    } else {
                        if (authCode == true) {
                            this.$refs.logBtn.click();
                        } else {
                            // alert('请进行人机验证');
                            this.$message({
                                message: '请先进行人机验证',
                                type: 'warning'
                            });
                            return false;
                        }
                    }
                });
            },
        }
    });

    // 验证码
    //一、定义一个获取DOM元素的方法
    var $ = function (selector) {
            return document.querySelector(selector);
        },
        box = $(".drag"),//容器
        bg = $(".bg"),//背景
        text = $(".text"),//文字
        btn = $(".btn"),//滑块
        success = false,//是否通过验证的标志
        distance = box.offsetWidth - btn.offsetWidth;//滑动成功的宽度（距离）
    //二、给滑块注册鼠标按下事件
    btn.onmousedown = fun;
    window.onkeydown = function (e) {
        if (e.keyCode == 13) {
            return false;
        }
    }

    function fun(e) {
        //1.鼠标按下之前必须清除掉后面设置的过渡属性
        btn.style.transition = "";
        bg.style.transition = "";
        //说明：clientX 事件属性会返回当事件被触发时，鼠标指针向对于浏览器页面(或客户区)的水平坐标。
        //2.当滑块位于初始位置时，得到鼠标按下时的水平位置
        var e = e || window.event;
        var downX = e.clientX;
        //三、给文档注册鼠标移动事件
        document.onmousemove = function (e) {
            var e = e || window.event;
            //1.获取鼠标移动后的水平位置
            var moveX = e.clientX;
            //2.得到鼠标水平位置的偏移量（鼠标移动时的位置 - 鼠标按下时的位置）
            var offsetX = moveX - downX;
            //3.在这里判断一下：鼠标水平移动的距离 与 滑动成功的距离 之间的关系
            if (offsetX > distance) {
                offsetX = distance;//如果滑过了终点，就将它停留在终点位置
            } else if (offsetX < 0) {
                offsetX = 0;//如果滑到了起点的左侧，就将它重置为起点位置
            }
            //4.根据鼠标移动的距离来动态设置滑块的偏移量和背景颜色的宽度
            btn.style.left = offsetX + "px";
            bg.style.width = offsetX + "px";
            //如果鼠标的水平移动距离 = 滑动成功的宽度
            if (offsetX == distance) {
                //1.设置滑动成功后的样式
                text.innerHTML = "验证通过";
                text.style.color = "#fff";
                btn.innerHTML = "&radic;";
                btn.style.color = "green";
                bg.style.backgroundColor = "lightgreen";
                //2.设置滑动成功后的状态
                success = true;
                //成功后，清除掉鼠标按下事件和移动事件（因为移动时并不会涉及到鼠标松开事件）
                btn.onmousedown = null;
                document.onmousemove = null;
                //3.成功解锁后的回调函数
                setTimeout(function () {
                    authCode = true;
                }, 100);
            }
        }
        //四、给文档注册鼠标松开事件
        document.onmouseup = function (e) {
            //如果鼠标松开时，滑到了终点，则验证通过
            if (success) {
                return;
            } else {
                //反之，则将滑块复位（设置了1s的属性过渡效果）
                btn.style.left = 0;
                bg.style.width = 0;
                btn.style.transition = "left 1s ease";
                bg.style.transition = "width 1s ease";
            }
            //只要鼠标松开了，说明此时不需要拖动滑块了，那么就清除鼠标移动和松开事件。
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
</script>
</body>
</html>
